package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfigDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReModelCustomConfigRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowReModelCustomConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowReModelCustomConfigExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowReModelCustomConfigDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author lilh
 * @date 2021/6/7 10:03
 */
@Repository
public class FlowReModelCustomConfigRepositoryImpl implements FlowReModelCustomConfigRepository {

    @Autowired
    private FlowReModelCustomConfigDAO modelCustomConfigDAO;

    @Override
    public boolean addModelCustomConfig(ModelCustomConfig modelCustomConfig) {
        FlowReModelCustomConfig entity = new FlowReModelCustomConfig();
        BeanCopierUtils.copyProperties(modelCustomConfig, entity);
        return modelCustomConfigDAO.insertSelective(entity) > 0;
    }

    @Override
    public boolean save(ModelCustomConfigDomain modelCustomConfigDomain) {
        FlowReModelCustomConfig modelCustomConfig = new FlowReModelCustomConfig();
        BeanCopierUtils.copyProperties(modelCustomConfigDomain,modelCustomConfig);
        modelCustomConfig.setCreateTime(new Date());
        return modelCustomConfigDAO.insert(modelCustomConfig) > 0;
    }

    @Override
    public List<ModelCustomConfig> listModelCustomConfig(String deploymentId, String nodeCode, String type) {
        FlowReModelCustomConfigExample query = new FlowReModelCustomConfigExample();
        FlowReModelCustomConfigExample.Criteria criteria = query.createCriteria().andDeploymentIdEqualTo(deploymentId);
        if(type != null){
            criteria.andTypeEqualTo(type);
        }
        if (StrUtil.isNotEmpty(nodeCode)) {
            criteria.andNodeCodeEqualTo(nodeCode);
        } else {
            criteria.andNodeCodeIsNull();
        }
        List<FlowReModelCustomConfig> configs = modelCustomConfigDAO.selectByExampleWithBLOBs(query);

        return configs.stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<ModelCustomConfigDomain> listModelConfigByDeployIdAndType(String deploymentId, String nodeCode, String type) {
        FlowReModelCustomConfigExample query = new FlowReModelCustomConfigExample();
        FlowReModelCustomConfigExample.Criteria criteria = query.createCriteria().andDeploymentIdEqualTo(deploymentId);
        criteria.andTypeEqualTo(type);
        if (StrUtil.isNotEmpty(nodeCode)) {
            criteria.andNodeCodeEqualTo(nodeCode);
        } else {
            criteria.andNodeCodeIsNull();
        }
        List<FlowReModelCustomConfig> configs = modelCustomConfigDAO.selectByExampleWithBLOBs(query);

        return configs.stream().map(this::convertToDoamain).collect(Collectors.toList());
    }

    @Override
    public List<ModelCustomConfigDomain> listModelConfigByParams(String deploymentId, String nodeCode, String type) {
        FlowReModelCustomConfigExample queryCondition = new FlowReModelCustomConfigExample();
        FlowReModelCustomConfigExample.Criteria criteria = queryCondition.createCriteria();

        // 不为空,加入查询条件
        if(StrUtil.isNotEmpty(deploymentId)) {
            criteria.andDeploymentIdEqualTo(deploymentId);
        }

        // 不为空,加入查询条件
        if(StrUtil.isNotEmpty(nodeCode)) {
            criteria.andNodeCodeEqualTo(nodeCode);
        }

        // 不为空,加入查询条件
        if (StrUtil.isNotEmpty(type)) {
            criteria.andTypeEqualTo(type);
        }

        // 查询范围数据并进行转换
        List<FlowReModelCustomConfig> configs = modelCustomConfigDAO.selectByExampleWithBLOBs(queryCondition);
        return configs.stream().map(this::convertToDoamain).collect(Collectors.toList());
    }

    private ModelCustomConfig convert(FlowReModelCustomConfig record) {
        if (Objects.isNull(record)) {
            return null;
        }
        ModelCustomConfig config = new ModelCustomConfig();
        BeanCopierUtils.copyProperties(record, config);
        return config;
    }

    private ModelCustomConfigDomain convertToDoamain(FlowReModelCustomConfig record){
        if (Objects.isNull(record)) {
            return null;
        }
        ModelCustomConfigDomain config = new ModelCustomConfigDomain();
        BeanCopierUtils.copyProperties(record, config);
        return config;
    }
}
